package com.aizhixin.test.logonAndlAnd.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aizhixin.test.logonAndlAnd.VO.FacePhoto;
import com.aizhixin.test.logonAndlAnd.VO.tokenZhiDeLinShi;
import com.aizhixin.test.logonAndlAnd.dto.userImformationDTO;
import com.aizhixin.test.logonAndlAnd.service.userImformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value = "/v1/open/userImformation")
@Api(tags = "注册和登陆所需API")
@Slf4j
public class userImformationController {
	@Autowired
	userImformationService userImformationService;
	/**
	 * 没有这个用户，
	 * 就直接加入信息，把这个直接，然后，等后面人脸识别的接口要用的时候再
	 * 把token值插入
	 * @return
	 */
	@PostMapping(value="/submit",produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod="POST",value="调用这个接口之后就可以完成注册（向数据库里添加数据）"
	,notes="调用这个接口之后就可以完成登陆或者注册的判断（向数据库里添加数据）<br><br><b>@author xixi</b>")
	public userImformationDTO logAndUp(
			@ApiParam(value = "用户信息和code值的接收,openId和token值不填", required = true) 
			@RequestBody userImformationDTO dto) {
		
		dto.setOpenId(userImformationService.getWeixinOpenInfo(dto.getCode()));//
		
		dto=userImformationService.findUser(dto.getOpenId(), dto);
		//为空则说明注册过的，可以直接登录
		log.info("==========注册用户========"+String.valueOf(dto));
		return dto;}
	
	  /**

     *<p>人脸注册</p> 
     * @Title: registrationFace    
     * @version:V0.1     
     * @param userimage 用户人脸注册的Base64人像数据
     * @throws Exception    
     * @return:String 返回注册结果(json对象)
     */
	  @PostMapping(value = "/userimage",produces = MediaType.APPLICATION_JSON_VALUE)
	    @ApiOperation(httpMethod = "POST",value = "首次人脸注册，获取token",notes = "根据传过来的best64，获取本人的token<br><br><b>@author xixi</b>")

	    public String registrationFace(
	    		@ApiParam(value = "人脸Base64数据",required = true)
	    		@RequestBody FacePhoto facePhoto ){
		  log.info("==========人脸注册========"+String.valueOf(facePhoto));
		  System.out.println("BUgq===" + facePhoto.getFace());
		  String aString=userImformationService.faceRe(facePhoto);
		  System.out.println("gsp" + aString);
	     return aString;
	    }
	    



 	/**
 	 *<p>将token存入数据库</p> 
 	 * @Title: registr    
 	 * @version:V0.1     
 	 * @param oneValueVO
 	 * @return:OneValueVO
 	 */
	   @PostMapping(value = "/tToken",produces = MediaType.APPLICATION_JSON_VALUE)
	    @ApiOperation(httpMethod = "POST",value = "将用户token写入数据库",
	    notes = "用户点击确定注册后将人脸的token保存在数据库<br><br><b>@author xixi</b>")
	    public tokenZhiDeLinShi registr(
	    		@ApiParam(value = "数据对象",required = true)
	    		@RequestBody tokenZhiDeLinShi tokenZhiDeLinShi) {
		   log.info("==========查看tokenZhiDeLshi里面是否写入了数据========"+String.valueOf(tokenZhiDeLinShi));
		   userImformationDTO dto=new userImformationDTO();
		   dto.setOpenId(tokenZhiDeLinShi.getOpenId());//将传进来的openid写进去
		   
		   dto.setToken(tokenZhiDeLinShi.getToken());//将
		   log.info("==========查看dto里面是否写入了数据========"+String.valueOf(dto));
		 userImformationService.addToken(dto);
	     
	     return tokenZhiDeLinShi;
	   }



 
 
 
 /**
  *<p>检测</p> 
  * @Title: searchFace    
  * @version:V0.1     
  * @param userimage 检测人脸数据
  * @return:String 返回检测数据
  */

	@PostMapping(value = "/FacePhoto", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "人脸检测登录", notes = "传入人脸数据<br><br><b>@author xixi</b>")
	public String searchFace(@ApiParam(value = "Base64", required = true) @RequestBody FacePhoto userimage) {
		log.info("==========查看tokenZhiDeLshi里面是否写入了数据========" + String.valueOf(userimage));
		return userImformationService.searchFace(userimage.getFace());
	}
}





